﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriculturalMachinery.Models;

namespace AgriculturalMachinery.Controllers
{
    public class NotifyController : Controller
    {
        //
        // GET: /Notify/
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index(int? page)
        {
            NotifyRepository nr = new NotifyRepository();
            var AllNotify = nr.FindAllNotify();
            var PagedAllNotify = new NotifyViewModel<Notify>(AllNotify, page ?? 0,7);
            //try
            //{
            //    string u = Request.Cookies["cooUserName"].Value;
            //    string i = Request.Cookies["cooUserID"].Value;
            //    if (u == null || i == null)
            //        Browse();
            //}
            //catch
            //{
            //    Browse();
            //}
            return View(PagedAllNotify);
        }

        public ActionResult Browse()
        {
            AccountRepository ar = new AccountRepository();
            ViewData["AllClients"] = ar.FindAllClients();

            NotifyRepository nr = new NotifyRepository();
            ViewData["AllNotify"] = nr.FindAllNotify();
            return View();
        }

        //
        // GET: /Notify/Details/5
        
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Notify/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Notify/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                NotifyRepository nr = new NotifyRepository();
                Notify n = new Notify();
                n.Announcer = Request.Params["Announcer"];
                n.NotifyTitle = Request.Params["NotifyTitle"];
                n.NotifyContent = Request.Params["NotifyContent"];
                n.PublicationDate = Convert.ToDateTime(Request.Params["PublicationDate"]);
                n.ReadTimes = 1;
                n.IsValid = 0;
                nr.AddNotify(n);

                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Notify/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Notify/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Notify/Delete/5
 
        public ActionResult Delete(int id)
        {
            NotifyRepository nr = new NotifyRepository();
            nr.DeleteNotify(id);
            return RedirectToAction("login", "admin");
        }

        //
        // POST: /Notify/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                string delString = Request.Params["delString"];
                string[] delStringSplit = delString.Split('|');
                NotifyRepository nr = new NotifyRepository();
                for (int i = 0; i < delStringSplit.Length; i++)
                {
                    nr.DeleteNotify(Convert.ToInt32(delStringSplit[i]));
                }//


                return RedirectToAction("login", "admin");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult page(int? page)
        {
            const int PageSize = 3;
            NotifyRepository nr = new NotifyRepository();
            var notify = nr.FindAllNotify().Skip((page ?? 0) * PageSize).Take(PageSize);

            return View(notify);
        }
    }
}
